package sebastiankerckhof.scene.geometry;

import java.util.ArrayList;
import java.util.List;

import sebastiankerckhof.math.Matrix4f;
import sebastiankerckhof.render.raytrace.Ray;
import sebastiankerckhof.render.raytrace.Hit.Hit;
import sebastiankerckhof.scene.material.Material;
import sebastiankerckhof.scene.texture.Texture;
import sebastiankerckhof.scene.transformation.TransformationStack;

public class Mesh implements Surface{
	private Material material;
	private Texture texture;
	private BVHNode triangles;
	private TransformationStack motion;

	public Mesh(List<Triangle> triangles){
		List<Surface> s = new ArrayList<Surface>();
		for(Triangle t: triangles){
			t.mesh = this;
			s.add(t);
		}
		
		this.triangles = new BVHNode(triangles,0);
	}
	
	@Override
	public Material getMaterial() {
		return this.material;
	}

	@Override
	public Texture getTexture() {
		return this.texture;
	}
	
	@Override
	public void setMaterial(Material material) {
		this.material = material;
	}

	@Override
	public void setTexture(Texture texture) {
		this.texture = texture;
	}

	@Override
  public void transform(Matrix4f M, Matrix4f N) {
	  this.triangles.transform(M, N);
  }

	@Override
  public boolean hit(Ray r, float t0, Hit h) {
	  return this.triangles.hit(r, t0, h);
  }

	BBox bbox;
	@Override
  public BBox getBBox() {
	  return this.triangles.getBBox();
  }

	@Override
  public float getMidPoint(int axis) {
	  return this.triangles.getMidPoint(axis);
  }

	@Override
  public void setMotion(TransformationStack t) {
	  this.motion = t;
  }

	@Override
  public void transformToStep(float step) {
		if(step != 1.0f && motion != null && motion.size() > 0){
			this.transform(motion.peekStep(step),motion.peekNormalStep(step));
		}
	  
  }

	@Override
  public void transformFromStep(float step) {
		if(step != 1.0f && motion != null && motion.size() > 0){
			this.transform(motion.peekNormalStep(step).transpose(),motion.peekStep(step).transpose());
		}
  }

}
